<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>管理员</span>
      </div>
      <div>
        <el-table
          :data="userList"
          style="width: 100%">
          <el-table-column
            label="用户名"
            width="180">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.username }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="密码"
            width="180">
            <template slot-scope="scope">
              <span size="medium">{{ maskPassword(scope.row.password) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="角色"
            width="180">
            <template slot-scope="scope">
              <span size="medium">{{ scope.row.role }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                width="30%"
                size="mini"
                @click="handleEdit(scope.$index, scope.row),dialogVisible = true">编辑</el-button>
                <el-dialog
                  title="提示"
                  :visible.sync="dialogVisible"
                  width="30%"
                  :before-close="handleClose">
                  <span>
                    <el-form :model="userForm" status-icon label-width="100px" class="demo-ruleForm">
                      <el-form-item label="账号" prop="username">
                        <el-input v-model="userForm.username"></el-input>
                      </el-form-item>
                      <el-form-item label="密码" prop="password">
                        <el-input type="password" v-model="userForm.password" autocomplete="off"></el-input>
                      </el-form-item>
                      <el-form-item label="角色" prop="role">
                        <el-input v-model="userForm.role" autocomplete="off"></el-input>
                      </el-form-item>
                    </el-form>
                  </span>
                  <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
                  </span>
                </el-dialog>
              <el-button
                width="30%"
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
  </div>
</template>
<script>
import { getUserListApi } from '@/api/index'
export default {
  data () {
    return {
      dialogVisible: false,
      userList: [
        { username: 'admin', password: '123456', role: '管理员' }
      ],
      userForm: {
        username: '',
        password: '',
        role: ''
      }
    }
  },
  created () {
    this.getUserList()
  },
  methods: {
    async getUserList () {
      const res = await getUserListApi()
      if (res.data.code === 200) {
        this.userList = res.data.data
      }
      console.log(res.data)
    },
    handleEdit (index, row) {
      console.log(index, row)
      this.dialogVisible = true
      this.userForm.username = row.username
      this.userForm.password = row.password
      this.userForm.role = row.role
    },
    handleDelete (index, row) {
      console.log(index, row)
    },
    maskPassword (password) {
      return '******'
    },
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    }
  }
}

</script>
<style lang="less" scoped>
  .box-card {
    width: 100%;
    line-height: 0;
  }
</style>
